//
//  XGTableView.m
//  两种Cell间的切换
//
//  Created by 小果 on 16/7/14.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "XGTableView.h"
#import "CellModel.h"
#import "TableCellFrame.h"
#import "TableViewCell.h"

@interface XGTableView ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation XGTableView
-(NSMutableArray *)dataArray{
    if (nil == _dataArray){
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DataSource.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];

    NSArray *dataAr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSMutableArray *mutable = [NSMutableArray array];

    [dataAr enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
        CellModel *model = [CellModel cellModelWithDict:dict];
        TableCellFrame *cellFrame = [[TableCellFrame alloc] init];
        cellFrame.cellModel = model;
        [mutable addObject:cellFrame];
    }];
        _dataArray = mutable;
    }
    return _dataArray;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.dataSource = self;
        self.delegate = self;
        self.backgroundColor = Color;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

#pragma mark - tableView的数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TableViewCell *cell = [TableViewCell tableCellWithTableView:tableView];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    TableCellFrame *cFrame = self.dataArray[indexPath.row];
    
    cell.cellFrame = cFrame;
    
    return cell;
    
}
#pragma mark - tableView的代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableCellFrame *cFrame = self.dataArray[indexPath.row];
    return cFrame.cellHeight;
}
#pragma mark - 滑动Cell时的操作
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 删除按钮
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        // 从模型数组中将选中的Cell的索引移除
        [self.dataArray removeObjectAtIndex:indexPath.row];
        // 从table中删除对应索引的行
        [self deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        // 刷新表格
        [self reloadData];
        NSLog(@"%zd",self.dataArray.count);
        
    }];
    action1.backgroundColor = [UIColor redColor];
    
    // 已读按钮
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"标为已读" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        TableCellFrame *cellF = self.dataArray[indexPath.row];
        // 方法一：
        cellF.bgValueF = CGRectZero;
        // 方法二：
        //        cellF.cellModel.value = @"0";
        [self reloadData];
        
        NSLog(@"===%@",cellF);
        
    }];
    action2.backgroundColor = [UIColor colorWithRed:255/255.0 green:194/255.0 blue:68/255.0 alpha:1.0];
    
    // 置顶按钮
    UITableViewRowAction *action3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        TableCellFrame *cellF = [[TableCellFrame alloc] init];
        // 1、取出要置顶行对应的模型数据
        cellF = self.dataArray[indexPath.row];
        // 2、将选中的行从数组中移除
        [self.dataArray removeObjectAtIndex:indexPath.row];
        // 3、将置顶行的模型数据插入到第一行
        [self.dataArray insertObject:cellF atIndex:0];
        
        [self reloadData];
        NSLog(@"%zd",self.dataArray.count);
        
    }];
    action3.backgroundColor = [UIColor lightGrayColor];
    
    return @[action1,action2,action3];
}
#pragma mark - Cell动画  willDisplayCell: cell将要显示时会调用的方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //缩放
    //    cell.layer.transform = CATransform3DMakeScale(0.7, 0.7, 1);
    //    [UIView animateWithDuration:1 animations:^{
    //        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    //    }];
    
    //旋转
    cell.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1);
    [UIView animateWithDuration:1 animations:^{
        cell.layer.transform = CATransform3DMakeRotation(0, 0, 0, 0);
    }];
    
}

@end
